package comtest.mongodb.mapper;

import org.springframework.data.mongodb.core.query.Criteria;

/**
 * @author: yxzhang23
 * @time: 2021/5/11
 */
public class MongoCriteria {

    private Criteria criteria = null;

    private MongoCriteria() {}

    public static MongoCriteria create() {
        return new MongoCriteria();
    }

    /**
     * equal方法，精准对比
     * @param filed 对比字段
     * @param o     对比内容
     */
    public MongoCriteria equal(String filed, Object o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).is(o);
            } else {
                this.criteria = Criteria.where(filed).is(o);
            }
        }
        return this;
    }

    /**
     * like方法，模糊对比
     * @param filed 对比字段
     * @param o     对比内容
     */
    public MongoCriteria like(String filed, String o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).regex(o);
            } else {
                this.criteria = Criteria.where(filed).regex(o);
            }
        }
        return this;
    }

    /**
     * in方法，判断是否字段是否在范围内
     * @param filed 对比字段
     * @param o     存在范围
     */
    public MongoCriteria in(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).in(o);
            } else {
                this.criteria = Criteria.where(filed).in(o);
            }
        }
        return this;
    }

    /**
     * notIn方法，排除不在范围内
     * @param filed 对比字段
     * @param o     存在范围
     */
    public MongoCriteria notIn(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).nin(o);
            } else {
                this.criteria = Criteria.where(filed).nin(o);
            }
        }
        return this;
    }

    /**
     * gt方法，判断大于o
     * @param filed 对比字段
     * @param o     小值
     */
    public MongoCriteria gt(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).gt(o);
            } else {
                this.criteria = Criteria.where(filed).gt(o);
            }
        }
        return this;
    }

    /**
     * gte方法，判断大于等于o
     * @param filed 对比字段
     * @param o     小值
     */
    public MongoCriteria gte(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).gte(o);
            } else {
                this.criteria = Criteria.where(filed).gte(o);
            }
        }
        return this;
    }

    /**
     * lt方法，判断小于o
     * @param filed 对比字段
     * @param o     大值
     */
    public MongoCriteria lt(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).lt(o);
            } else {
                this.criteria = Criteria.where(filed).lt(o);
            }
        }
        return this;
    }

    /**
     * lte方法，判断小于等于o
     * @param filed 对比字段
     * @param o     大值
     */
    public MongoCriteria lte(String filed, Object ... o) {
        if (null != filed && null != o) {
            if (null != criteria) {
                this.criteria.and(filed).lte(o);
            } else {
                this.criteria = Criteria.where(filed).lte(o);
            }
        }
        return this;
    }

    /**
     * 获取条件对象
     */
    public Criteria get() {
        return this.criteria;
    }

}
